package com.coderman.infosys.auth.app.executor;

import com.coderman.infosys.auth.app.command.AbstractCmd;
import com.coderman.infosys.auth.app.command.AuthToRoleCmd;
import com.coderman.infosys.auth.domain.enums.errors.AuthorityErrorEnum;
import com.coderman.infosys.auth.domain.repository.AuthorityRepository;

import com.coderman.infosys.plat.auth.common.behavior.AuthorityOpEnum;
import com.coderman.infosys.plat.auth.common.behavior.IAppOperation;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:角色权限cmd处理类接口实现类
 * @Author：2022-01-02
 * @CreateTime：2022-01-02 15:27:38
 * @version v1.0
 */
@Service
public class RoleAuthCmdExe  implements  CmdExeService{

	private  Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AuthorityRepository authorityRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
	public ResultDataDto exe(IAppOperation appOperation, AbstractCmd abstractCmd){
        AuthToRoleCmd addRoleAuthCmd = (AuthToRoleCmd) abstractCmd;

        if(AuthorityOpEnum.createAuthToRole(appOperation.getOperationCode())){
            authorityRepository.authToRole(addRoleAuthCmd.buildAuthorityBO());
        }
        else if(AuthorityOpEnum.updateAuthToRole(appOperation.getOperationCode())){
            authorityRepository.updateAuthToRole(addRoleAuthCmd.buildAuthorityBO());
        }
        return ResultDataDto.success();
    }

}